package com.atguigu.gmall.common.config;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一将微服务产生异常进行处理
 *
 * @author: atguigu
 * @create: 2023-04-17 09:35
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理运行时异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public Result exceptionError(RuntimeException e) {
        log.error("全局异常RuntimeException捕获:{}", e);
        return Result.fail().code(ResultCodeEnum.FAIL.getCode()).message(e.getMessage());
    }

    /**
     * 处理各种类型异常
     */
    @ExceptionHandler(Exception.class)
    public Result error(Exception e) {
        log.error("异常信息:{}", e);
        return Result.fail().code(ResultCodeEnum.FAIL.getCode()).message(e.getMessage());
    }


}
